<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

/**
 * Create forms in OOP style
 * @author Anthony Gallon
 * @package  JFX
 */

/**
 * Create forms in OOP style
 * @author Anthony Gallon
 * @package  jfx-core-classes
 */

class JFX_Form extends JFX_Form_Component
{
    /**
     * Array containing objects of type JFX_Form_Block
     *
     * @var array $blocks
     */
    protected $blocks = array();
    /**
     * Set the enctype attribute for the form tag
     *
     * @var string $encType
     */
    protected $encType = '';
    /**
     * Determine the namespace for intelliform
     *
     * @var string $intelliFormId
     */
    protected $intelliFormId='default';

    /**
     * Set the form properties
     *
     * @param string $intelliFormId
     */
    public function __construct($intelliFormId='default'){
        $id = 'form'.rand();
        $this->intelliFormId = $intelliFormId;
        $this->setId($id);
        $this->setMethod();
        $this->setAction(THIS_PAGE_URL);
    }


	/**
	 * Set the enctype attribute
	 *
	 * @param string $encType
	 */
	public function setEncType($encType){
		$this->encType = $encType;
	}

    /**
     * Get the enctype for this form, even if empty string
     *
     * @return string $encType
     */
    public function getEncType(){
    	if($this->encType == '') return '';
        return ' enctype="'.$this->encType.'" ';
    }

    /**
     * Set the form action ( by default it is derived from the constant THIS_PAGE_URL
     *
     * @param string $url
     */
    public function setAction($url){
        $this->action = $url;
    }

    /**
     * Set the form method (get or post)
     *
     * @param string $method
     */
    public function setMethod($method='post'){
        $this->method = $method;
    }


    /**
     * Adds a block to the form, blocks hold fieldsets
     *
     * @param string $colType "full" or "double"
     * @param string $id
     * @return object JFX_Form_Block
     */
    public function add($colType, $id=''){
        if($id=='')$id = $this->id.'-block-'.rand();
        $key = count($this->blocks);
        $this->blocks[$key] = new JFX_Form_Block($colType, $id);
        $this->blocks[$key]->formObj = $this;
        return $this->blocks[$key];
    }

    /**
     * Enables you to add a custom object after the last form block. Object must implement method "render"
     *
     * @param object $obj
     */
    public function addCustomObject($obj){
        if(!method_exists($obj, 'render')) die('not defined');
        $this->blocks[] = $obj;
    }

    /**
     * Convert this form to html for output to the browser
     *
     * @return string $htmlFormString
     */
    public function render(){
        $returnString = '<form method="'.$this->method.'" action="'.$this->action.'" '.
        	$this->getEncType().' id="'.$this->getId().'" '.$this->renderClasses().' '.
        	$this->renderParameters().'>';
        $returnString .= Antz_IntelliForm::seed($this->intelliFormId);
        foreach($this->blocks as $block){
            $returnString .= "\n  ".$block->render();
        };
        $returnString .= "\n".'</form>';
        return $returnString;
    }
     
   /**
    * Get the form as HTML
    *
    * @return string $htmlFormString
    */
   public function __toString(){
   	return $this->render();
   }

    /**
     * Add a block with a fieldset containing the submit button
     * Useful to add this before the first form block, capture the returned object and use 
     * addCustomObject() to create a submit button at top and bottom of form
     *
     * @param string $val
     * @return object JFX_Form_Block
     */
    public function addSubmit($val = 'Submit changes >>'){
        $submitBlock = $this->add('full');
        $submitBlock->addClass('bold-submit');
        $fs = $submitBlock->add('');
        $fs->showBorder(false);
        $fs->addInput('submit', 'submitbtn', '&nbsp;', $val);
        return $submitBlock;
    }

    /**
     * Adds a button to ask a question using javascript "confirm" function and call
     * a custom javascript function if successful
     *
     * @param string $val
     * @param string $question
     * @param string $callbackFunctions
     * @param string $customFunction
     * @return object JFX_Form_Components_Input_Submit
     */
    public function addJsSubmit($val = 'Submit changes >>', $question='', $callbackFunctions = '', $customFunction = ''){
        if($question == ''){
            $question = 'Are you sure you want to do this?';
        };
        $submitBlock = $this->add('full');
        $submitBlock->addClass('bold-submit');
        $fs = $submitBlock->add('');
        $fs->showBorder(false);
        $btn = $fs->addInput('submit', 'submit', '&nbsp;', $val);
        $btn->setId('submit');
        if($customFunction == ''){
            $this->addJsEvent('onsubmit', 'return JFXBase.confirmSubmit(\''.$question.'\', \''.$callbackFunctions['yes'].'\', \''.$callbackFunctions['no'].'\')');
        }else{
            $this->addJsEvent('onsubmit', 'return JFXBase.customConfirmSubmit(\''.$customFunction.'\', \''.$callbackFunctions['yes'].'\', \''.$callbackFunctions['no'].'\')');
        }
        return $submitBlock;
    }

    /**
     * Add two buttons, one submits the form, the other redirects to a provided url
     *
     * @param string $lastPage url
     * @param string $confVal
     * @param string $cancelVal
     * @return object JFX_Form_Block
     */
    public function addConfirmCancel($lastPage, $confVal='Confirm >>', $cancelVal='<< Cancel'){
        $submitBlock = $this->add('full');
        $submitBlock->addClass('bold-submit');
        $fs = $submitBlock->add('');
        $fs->showBorder(false);
        $cancel = $fs->addInput('button', 'cancel', '', $cancelVal);
        $cancel->addJsEvent('onclick', 'window.location=\''.$lastPage.'\'');
        $cancel->showLabel(false);
        $cancel->setWrap(false);
        $submit = $fs->addInput('submit', 'submitbtn', '&nbsp;', $confVal);
        $submit->showLabel(false);
        $submit->setWrap(false);
        return $submitBlock;
    }

}
